﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class AllocateRuleDetailBLL : IBaseInfoDetail
    {
        private static readonly IAllocateRuleDetailDA alloccateRuleDetailDA = DataAccess.CreateAllocateRuleDetailDA();
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;

        public AllocateRuleDetailBLL()
        {
            this.pubParasInfo = null;
        }

        public AllocateRuleDetailBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public AllocateRuleDetailBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteDetail(List<string> allocateRuleID, List<string> lineID, List<string> pk3)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                if (allocateRuleID.Count != lineID.Count)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2440_004", this.pubParasInfo.UserLanguage);
                }
                List<AllocateRuleDetailInfo> allocateRuleDetailInfos = new List<AllocateRuleDetailInfo>();
                AllocateRuleDetailInfo allocateRuleDetailInfo = null;
                foreach (string id in lineID)
                {
                    allocateRuleDetailInfo = alloccateRuleDetailDA.GetAllocateRuleDetailByID(dataBase, tran, allocateRuleID[0], id, this.dataBaseID[0], this.pubParasInfo);
                    allocateRuleDetailInfos.Add(allocateRuleDetailInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (alloccateRuleDetailDA.DeleteAllocateRuleDetail(dataBase, tran, allocateRuleID, lineID, this.dataBaseID) > 0)
                {
                    for (int i = 0; i < allocateRuleID.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<AllocateRuleDetailInfo>(dataBase, tran, allocateRuleDetailInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "ALLOCATE_RULE_DETAIL", this.dataBaseID[0]);
                        string CS00003 = temp;
                        temp = CS00003 + allocateRuleID[i] + "," + lineID[i] + ";";
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (temp.Length > 0)
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return "";
        }

        public DataSet GetAllDetailInfo()
        {
            return alloccateRuleDetailDA.GetAllAllocateRuleDetail(this.dataBaseID[0], this.pubParasInfo);
        }

        public DataSet GetDetaiInfolByQueryEntity(object obj)
        {
            AllocateRuleDetailQueryEntity allocateRuleDetail = obj as AllocateRuleDetailQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(allocateRuleDetail.AllocateRuleID))
            {
                sqlWhere.Add(" ALLOCATE_RULE_ID " + allocateRuleDetail.AllocateRuleID);
            }
            return alloccateRuleDetailDA.GetAllocateRuleDetailByQueryList(sqlWhere, allocateRuleDetail, this.dataBaseID[0], this.pubParasInfo);
        }

        public object GetDetailInfoByID(string allocateRuleID, string lineID, string pk3)
        {
            return alloccateRuleDetailDA.GetAllocateRuleDetailByID(allocateRuleID, lineID, this.dataBaseID[0], this.pubParasInfo);
        }

        public List<object> GetDetailInfoListByID(string allocateRuleID, string subID)
        {
            List<AllocateRuleDetailInfo> allocateRuleDetail = alloccateRuleDetailDA.GetAllocateRuleDetailListByID(allocateRuleID, this.dataBaseID[0], this.pubParasInfo);
            List<object> objs = null;
            if ((allocateRuleDetail != null) && (allocateRuleDetail.Count > 0))
            {
                objs = new List<object>();
                for (int i = 0; i < allocateRuleDetail.Count; i++)
                {
                    objs.Add(allocateRuleDetail[i]);
                }
            }
            return objs;
        }

        public string InsertDetail(object obj)
        {
            AllocateRuleDetailInfo allocateRuleDetailInfo = obj as AllocateRuleDetailInfo;
            string lineID = string.Empty;
            try
            {
                if (allocateRuleDetailInfo == null)
                {
                    return lineID;
                }
                if (alloccateRuleDetailDA.CheckAllocateRuleDetailIDUnique(allocateRuleDetailInfo.AllocateRuleID, allocateRuleDetailInfo.LineID.ToString(), this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2440_003", this.pubParasInfo.UserLanguage, new List<string> { allocateRuleDetailInfo.AllocateRuleID, allocateRuleDetailInfo.LineID.ToString() });
                }
                allocateRuleDetailInfo.UpdatedBy = allocateRuleDetailInfo.CreatedBy;
                allocateRuleDetailInfo.CreatedDate = DateTime.Now;
                allocateRuleDetailInfo.UpdatedDate = DateTime.Now;
                if (alloccateRuleDetailDA.InsertAllocateRuleDetail(allocateRuleDetailInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<AllocateRuleDetailInfo>(null, allocateRuleDetailInfo, AuditAction.Insert, this.pubParasInfo.UserID, "ALLOCATE_RULE_DETAIL", this.dataBaseID[0]);
                    lineID = allocateRuleDetailInfo.LineID.ToString();
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return lineID;
        }

        public string UpdateDetail(object obj)
        {
            string LineID = string.Empty;
            try
            {
                AllocateRuleDetailInfo allocateRuleDetailInfo = obj as AllocateRuleDetailInfo;
                allocateRuleDetailInfo.UpdatedBy = this.pubParasInfo.UserID;
                allocateRuleDetailInfo.UpdatedDate = DateTime.Now;
                AllocateRuleDetailInfo oldAllocateRuleDetailInfo = alloccateRuleDetailDA.GetAllocateRuleDetailByID(allocateRuleDetailInfo.AllocateRuleID, allocateRuleDetailInfo.LineID.ToString(), this.dataBaseID[0], this.pubParasInfo);
                if (alloccateRuleDetailDA.UpdateAllocateRuleDetail(allocateRuleDetailInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<AllocateRuleDetailInfo>(oldAllocateRuleDetailInfo, allocateRuleDetailInfo, AuditAction.Update, this.pubParasInfo.UserID, "ALLOCATE_RULE_DETAIL", this.dataBaseID[0]);
                    LineID = allocateRuleDetailInfo.AllocateRuleID + " , " + allocateRuleDetailInfo.LineID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return LineID;
        }

        public string UpdateIsActive(List<string> mainID, List<string> subId, string isActive)
        {
            throw new NotImplementedException();
        }
    }
}

